<h2>DESCRIPTION</h2>

<p><em>r.terraflow</em> takes as input a raster digital elevation
model (DEM) and computes the flow direction raster and the flow
accumulation raster, as well as the flooded elevation raster,
sink-watershed raster (partition into watersheds around sinks) and TCI
(topographic convergence index) raster maps.

<p><em>r.terraflow</em> computes these rasters using well-known
approaches, with the difference that its emphasis is on the
computational complexity of the algorithms, rather than on modeling
realistic flow.  <em>r.terraflow</em> emerged from the necessity of
having scalable software able to process efficiently very large
terrains.  It is based on theoretically optimal algorithms developed
in the framework of I/O-efficient algorithms.  <em>r.terraflow</em>
was designed and optimized especially for massive grids and is able to
process terrains which were impractical with similar functions
existing in other GIS systems.

<p>Flow directions are computed using either the MFD (Multiple Flow
Direction) model or the SFD (Single Flow Direction, or D8) model,
illustrated below. Both methods compute downslope flow directions by
inspecting the 3-by-3 window around the current cell. The SFD method
assigns a unique flow direction towards the steepest downslope
neighbor. The MFD method assigns multiple flow directions towards all
downslope neighbors.

<p><table width="80%" align=center>
 <tr>
  <th><img src="rterraflow_dir2.png" alt="r.terraflow SFD"></th>
  <th><img src="rterraflow_dir3.png" alt="r.terraflow MFD"></th>
 </tr>
 <tr>
  <th>Flow direction to steepest<br> downslope neighbor (SFD).</th>
  <th>Flow direction to all<br> downslope neighbors (MFD).</th>
 </tr>
</table>


<p>The SFD and the MFD method cannot compute flow directions for
cells which have the same height as all their neighbors (flat areas)
or cells which do not have downslope neighbors (one-cell pits).

<ul>
  <li>On plateaus (flat areas that spill out) <em>r.terraflow</em>
routes flow so that globally the flow goes towards the spill cells of
the plateaus.

  <li>On sinks (flat areas that do not spill out, including one-cell
pits) <em>r.terraflow</em> assigns flow by flooding the terrain until
all the sinks are filled and assigning flow directions on the filled
terrain.
</ul>

<p>In order to flood the terrain, <em>r.terraflow</em> identifies all
sinks and partitions the terrain into sink-watersheds (a
sink-watershed contains all the cells that flow into that sink),
builds a graph representing the adjacency information of the
sink-watersheds, and uses this sink-watershed graph to merge
watersheds into each other along their lowest common boundary until
all watersheds have a flow path outside the terrain. Flooding produces
a sink-less terrain in which every cell has a downslope flow path
leading outside the terrain and therefore every cell in the terrain
can be assigned SFD/MFD flow directions as above.

<p>Once flow directions are computed for every cell in the terrain,
<em>r.terraflow</em> computes flow accumulation by routing water using
the flow directions and keeping track of how much water flows through
each cell.

<p>If flow accumulation of a cell is larger than the value given by the
<b>d8cut</b> option, then
the flow of this cell is routed to its neighbors using the SFD (D8)
model. This option affects only the flow accumulation raster and is
meaningful only for MFD flow (i.e. if the <b>-s</b> flag is not used); If
this option is used for SFD flow it is ignored. The default value of
<b>d8cut</b> is <i>infinity</i>.

<p><em>r.terraflow</em> also computes the <b>tci</b> raster (topographic convergence
index, defined as the logarithm of the ratio of flow accumulation and
local slope).

<p>For more details on the algorithms see [1,2,3] below.


<h2>NOTES</h2>

One of the techniques used by <em>r.terraflow</em> is the
space-time trade-off. In particular, in order to avoid searches, which
are I/O-expensive, <em>r.terraflow</em> computes and works with an
augmented elevation raster in which each cell stores relevant
information about its 8 neighbors, in total up to 80B per cell.  As a
result <em>r.terraflow</em> works with intermediate temporary files
that may be up to 80N bytes, where N is the number of cells (rows x
columns) in the elevation raster (more precisely, 80K bytes, where K
is the number of valid (not no-data) cells in the input elevation
raster).
<p>All these intermediate temporary files are stored in the path specified
by the <b>directory</b> option. Note: <b>directory</b> must contain
enough free disk space in order to store up to 2 x 80N bytes.

<p>The <b>memory</b> option can be used to set the maximum amount of main
memory (RAM) the module will use during processing. In practice its
<i>value</i> should be an underestimate of the amount of available
(free) main memory on the machine. <em>r.terraflow</em> will use at
all times at most this much memory, and the virtual memory system
(swap space) will never be used. The default value is 300 MB.

<p>The <b>stats</b> option defines the name of the file that contains the
statistics (stats) of the run.

<p><em>r.terraflow</em> has a limit on the number of rows and columns
(max 32,767 each).

<p>The internal type used by <em>r.terraflow</em> to store elevations
can be defined at compile-time. By default, <em>r.terraflow</em> is
compiled to store elevations internally as floats. Other versions can be
created by the user if needed.

<p>Hints concerning compilation with storage of elevations internally as
shorts: such a version uses less space (up to 60B per cell, up
to 60N intermediate file) and therefore is more space and time
efficient. <em>r.terraflow</em> is intended for use with floating
point raster data (FCELL), and <em>r.terraflow (short)</em> with integer
raster data (CELL) in which the maximum elevation does not exceed the
value of a short SHRT_MAX=32767 (this is not a constraint for any
terrain data of the Earth, if elevation is stored in meters).
Both <em>r.terraflow</em> and <em>r.terraflow (short)</em> work with
input elevation rasters which can be either integer, floating point or
double (CELL, FCELL, DCELL). If the input raster contains a value that
exceeds the allowed internal range (short for
<em>r.terraflow (short)</em>, float for <em>r.terraflow</em>), the
program exits with a warning message. Otherwise, if all values in the
input elevation raster are in range, they will be converted
(truncated) to the internal elevation type (short for
<em>r.terraflow (short)</em>, float for <em>r.terraflow</em>). In this
case precision may be lost and artificial flat areas may be created.
For instance, if <em>r.terraflow (short)</em> is used with floating
point raster data (FCELL or DCELL), the values of the elevation will
be truncated as shorts. This may create artificial flat areas, and the
output of <em>r.terraflow (short)</em> may be less realistic than those
of <em>r.terraflow</em> on floating point raster data.
The outputs of <em>r.terraflow (short)</em> and <em>r.terraflow</em> are
identical for integer raster data (CELL maps).


<h2>EXAMPLES</h2>

Example for small area in North Carolina sample dataset to calculate flow accumulation:
<div class="code"><pre>
g.region raster=elev_lid792_1m
r.terraflow elevation=elev_lid792_1m accumulation=elev_lid792_1m_accumulation
</pre></div>

<div align="center" style="margin: 10px">
<img src="rterraflow_accumulation.png" border=0><br>
<i>Flow accumulation</i>
</div>
<!--
image generated using
d.mon wx0
d.rast elev_lid792_1m
d.rast elev_lid792_1m_accumulation
save image to files
crop the background using Gimp or ImageMagic
mogrify -trim *.png
some bounding box problems noticed when opening mogrify result in Gimp
-->

<p>
Spearfish sample data set:

<div class="code"><pre>
g.region raster=elevation.10m -p
r.terraflow elev=elevation.10m filled=elevation10m.filled \
    dir=elevation10m.mfdir swatershed=elevation10m.watershed \
    accumulation=elevation10m.accu tci=elevation10m.tci
</pre></div>

<div class="code"><pre>
g.region raster=elevation.10m -p
r.terraflow elev=elevation.10m filled=elevation10m.filled \
    dir=elevation10m.mfdir swatershed=elevation10m.watershed \
    accumulation=elevation10m.accu tci=elevation10m.tci d8cut=500 memory=800 \
    stats=elevation10mstats.txt 
</pre></div>


<h2>REFERENCES</h2>

<ol>
  <li> The <a href="http://www.cs.duke.edu/geo*/terraflow/">TerraFlow</a> project at Duke University
  <li><A NAME="arge:drainage"
       HREF="http://www.cs.duke.edu/geo*/terraflow/papers/alenex00_drainage.ps.gz">
       I/O-efficient algorithms for problems on grid-based
       terrains</a>.  Lars Arge, Laura Toma, and Jeffrey S. Vitter. In
       <em>Proc. Workshop on Algorithm Engineering and Experimentation</em>,
       2000. To appear in <em>Journal of Experimental Algorithms</em>.
       
  <li><A NAME="terraflow:acmgis01"
       HREF="http://www.cs.duke.edu/geo*/terraflow/papers/acmgis01_terraflow.pdf">
       Flow computation on massive grids</a>.
       Lars Arge, Jeffrey S. Chase, Patrick N. Halpin, Laura Toma,
       Jeffrey S. Vitter, Dean Urban and Rajiv Wickremesinghe. In
       <em>Proc. ACM Symposium on Advances in Geographic Information
       Systems</em>, 2001.
       
  <li><A NAME="terraflow:geoinformatica"
       HREF="http://www.cs.duke.edu/geo*/terraflow/papers/journal_terraflow.pdf">
       Flow computation on massive grid terrains</a>.
       Lars Arge, Jeffrey S. Chase, Patrick N. Halpin, Laura Toma,
       Jeffrey S. Vitter, Dean Urban and Rajiv Wickremesinghe.
       In <em>GeoInformatica, International Journal on
       Advances of Computer Science for Geographic Information
       Systems</em>, 7(4):283-313, December 2003.
</ol>

<h2>SEE ALSO</h2>

<em>
<a href="r.flow.html">r.flow</a>,
<a href="r.basins.fill.html">r.basins.fill</a>,
<a href="r.drain.html">r.drain</a>,
<a href="r.topidx.html">r.topidx</a>,
<a href="r.topmodel.html">r.topmodel</a>,
<a href="r.water.outlet.html">r.water.outlet</a>,
<a href="r.watershed.html">r.watershed</a>
</em>

<h2>AUTHORS</h2>

<dl>
  <dt>Original version of program: The
	<a href="http://www.cs.duke.edu/geo*/terraflow/">TerraFlow</a> project,
	1999, Duke University.
	<dd><a href="http://www.daimi.au.dk/~large/">Lars Arge</a>,
		<a href="http://www.cs.duke.edu/~chase/">Jeff Chase</a>,
        <a href="http://www.env.duke.edu/faculty/bios/halpin.html">Pat Halpin</a>,
        <a href="http://www.bowdoin.edu/~ltoma/">Laura Toma</a>,
        <a href="http://www.env.duke.edu/faculty/bios/urban.html">Dean Urban</a>,
        <a href="http://www.science.purdue.edu/jsv/">Jeff Vitter</a>,
        Rajiv Wickremesinghe.
       
  <dt>Porting to GRASS GIS, 2002:
    <dd> <a href="http://www.daimi.au.dk/~large/">Lars Arge</a>,
	     <a href="http://www4.ncsu.edu/~hmitaso/index.html">Helena Mitasova,</a>
		 <a href="http://www.bowdoin.edu/~ltoma/">Laura Toma</a>. 
	   
	<dt>Contact: <a href="mailto:ltoma@bowdoin.edu "> Laura Toma</a></dt>
</dl>

<!--
<p>
<i>Last changed: $Date$</i>
-->
